// pages/vehicleStatus/index.js
import requestUtil from '../../baseRequest'
import urls from '../../requestUrl'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusTypes: [{
                name: '无',
                value: '0',
                checked: true
            },
            {
                name: '划痕',
                value: '1',
            },
            {
                name: '损伤',
                value: '2',
            }
        ],
        tab1Form: [],
        tab2Form: [],
        tab1: {
            list: [{
                    key: 'clqf',
                    title: '车辆前方'
                },
                {
                    key: 'clhf',
                    title: '车辆后方'
                },
                {
                    key: 'cldb',
                    title: '车辆顶部'
                },
            ],
            currentTab: 'clqf'
        },
        tab2: {
            list: [{
                    key: 'clyc',
                    title: '车辆右侧'
                },
                {
                    key: 'clzc',
                    title: '车辆左侧'
                }
            ],
            currentTab: 'clyc'
        },
        tempVal: {},
        name: ''
    },

    checkboxChange({ detail }) {
        console.log('checkboxChange', detail)
        let { value } = detail
        if (value[value.length - 1] != '0') {
            let index = value.indexOf('0')
                // index!=-1?de
        }
    },

    /**切换Tab1 */
    handleTab1Change({ detail }) {
        this.setData({
            ['tab1.currentTab']: detail.key
        })
    },

    /**切换Tab2 */
    handleTab2Change({ detail }) {
        this.setData({
            ['tab2.currentTab']: detail.key
        })
    },

    /**点击提交表单 */
    handleSubmit({ detail }) {
        console.log('form', detail)
        wx.showModal({
            title: '温馨提示',
            content: '是否确认提交车辆状况描述表单',
            success: res => {
                if (res.confirm) {
                    let targetValue = this.translateFormData(detail.value)
                        // getApp().globalData.vehicleStatus[this.data.name] = targetValue
                    getApp().saveVehicleStatus(this.data.name, targetValue)
                    wx.navigateBack()
                }
            }
        })
    },
    /**
     * 转换表单数据 
     */
    translateFormData(data) {
        let target = []
        for (let i in data) {
            let p = { "key": i, "value": data[i] }
            target.push(p)
        }
        return target
    },
    /**数据初始化转换 */
    parseValue(v) {
        console.log('parseValue', v)
        let target = {}
        v.forEach((item) => {
            target[item.key] = item.value
        })
        console.log('parseValue', target)
        return target
    },

    /**获取车况表单*/
    getVehicleStatusForm() {
        requestUtil.getRequest(urls.workOrder.getVehicleStatus, {}, (res) => {
            this.setData({
                tab1Form: res.tab1Form,
                tab2Form: res.tab2Form
            })
        }, () => {})
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options)
        let name = options.name
        let globalVehicleStatus = getApp().getVehicleStatus()
        console.log('globalVehicleStatus', globalVehicleStatus)
        let v = globalVehicleStatus[name]
        console.log('v', v)
        this.setData({
                name: name,
                tempVal: this.parseValue(v),
                isDisabled: options.disabled || false
            })
            //
        this.getVehicleStatusForm()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})